<template>
	<view>
		<!-- <back-fixed title="修改手机号" urlBack='/pages/my/index'></back-fixed> -->
		<view class="main">
			<view class="tips">
				修改后，可使用新手机号登录当前账号
			</view>
			<view class="">
				<view class="phone">
					<u-input :custom-style="inputFont" v-model="phone" type="number" placeholder="请输入手机号" />
				</view>
				<view class="message">
					<view class="number">
						<u-input v-model="code" :custom-style="inputFont" type="number" :clearable="false"
							placeholder="请输入短信验证码" />
					</view>
					<view class="getNumber" :class="{count:lastTime !== 0 && lastTime !==60}" @click="getCode()">
						{{lastTime > 0 && lastTime !== 60 ? lastTime+'s后重新' : '' }}获取验证码
					</view>
				</view>
			</view>
			<view class="login_button" @click="login">
				<view class="login_but_style" v-if="!actives">
					完成
				</view>
				<view class="login_but_style active" v-if="actives">
					完成
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		changePhoneNumber,
		userInfoPost,
		updReplenishUserInfo
	} from '@/api/index.js'
	export default {
		data() {
			return {
				inputFont: {
					'fontSize': '32rpx'
				},
				phone: '', //手机号
				code: '', //短信验证码
				lastTime: 60,
				countdown: null,
				flag: false,
			}
		},
		onLoad() {

		},
		computed: {
			actives() {
				if (this.phone.length == 11 && this.code.length == 6) {
					return true
				}
				return false
			}
		},
		methods: {
			async getCode() {
				if (this.phone.length !== 11) {
					this.$u.toast('手机号错误')
					return
				}
				let res = await changePhoneNumber({
					phoneNumber: this.phone
				})
				if (res.code == 200) {
					this.$u.toast(res.data)
					this.$u.throttle(this.startCountdown, 1000)
				}
				//console.log(res);
			},
			startCountdown() {
				//console.log(this.lastTime,this.flag);
				//if(this.countdown)return
				if (this.lastTime !== 60 || this.flag) return
				this.countdown = setInterval(() => {
					if (this.lastTime >= 1) {
						this.flag = true
						this.lastTime--
						console.log(this.lastTime + " 秒后结束");
					} else {
						clearInterval(this.countdown);
						this.lastTime = 60
						this.flag = false
						console.log("倒计时结束!");
					}
				}, 1000);
			},
			async login() {
				if (this.phone.length !== 11) {
					this.$u.toast('手机号错误')
					return
				}
				if (this.code.length !== 6) {
					this.$u.toast('验证码错误')
					return
				}
				let local = uni.getStorageSync('userinfo')
				let params = {
					'phoneNumber': this.phone,
					'nickName': local.user.nickName,
					'deptId': local.user.dept.deptId,
					'sex': local.user.sex,
					'smsCode': this.code,
					'userTitle': local.user.userTitle,

				}
				//console.log(params,11118);
				let res = await updReplenishUserInfo({
					...params
				})
				if (res.code == 200) {
					//uni.removeStorageSync('qw'); 
					uni.removeStorageSync('token'); //清除缓存
					uni.removeStorageSync('userinfo');
					if (res.code == 200) {
						//console.log(res.msg);
						this.$u.toast('修改成功，正在退出登录')
						setTimeout(() => {
							this.$u.route('/pages/login/index') //去登录
						}, 2000)
						//this.$u.route('/pages/index/index?to=0') //去首页
					}
				}
				// console.log(this.phone);
				// return

			}
		},
	}
</script>

<style lang="scss" scoped>
	.main {
		//padding-top: 60px;

		.tips {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 30rpx;
			color: #4DC0D9;
			line-height: 36px;
			background: #EEFCFF;
			padding: 24rpx 0 24rpx 24rpx;
		}

		.active {
			background-color: #4DC0D9 !important;
		}

		.phone {
			margin: 40rpx 24rpx;
			border-bottom: 1px solid #F8F8F8;
		}

		.message {
			margin: 40rpx 24rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1px solid #F8F8F8;

			//padding-right: 60rpx;
			.number {}

			.getNumber {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #4DC0D9;
				line-height: 38rpx;
			}

			.count {
				color: #C1C1C1 !important;
			}


		}

		.login_button {
			margin-top: 60rpx;
			margin-left: 24rpx;
			margin-right: 24rpx;
			font-size: 32rpx;

			.login_but_style {
				width: 100%;
				height: 84rpx;
				background: #A6DFEC;
				border-radius: 20rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: white;
			}
		}
	}
</style>